var _discussions = [], _discussionMap = {}, _currId = 0;

exports.list = function(onResults){
  onResults(_discussions);
};

exports.get = function(id, onResult) {
    var task;
    
    if (id) 
    {
    	console.log('api::get: ' + id);
    	result = _discussionMap[id];
    	console.log('api::get::result: ' + JSON.stringify(result));
    }

	onResult(result);
}

exports.add = function(task, onResult) {
    var result;
    
    if (task)
    {
    	task.id = ++_currId;
    	_discussions.push(task);
    	_discussionMap[_currId] = task;
        result = task; 	
    }
	
	onResult(result);
};

exports.delete = function(id, onResult) {
    var result = true;  // ignore if it doesn't exist
    if (id)
    {
        var idx = 0;
        for (; idx < _discussions.length; ++idx)
        {
            if (_discussions[idx].id == id)
            {
                _discussions.splice(idx, 1);
                break;
            }
        }
        delete _discussionMap[id];
    }

    onResult(result);
}
